package com.chinasofti.info_manager.dao;

import com.chinasofti.info_manager.domain.Teacher;

public class TeacherDao {
    private static Teacher[] teas = new Teacher[5];

    //查询id是否存在 index = -1 表示id 不存在
    public  int    getIndex(String id){
        int index = -1;
        for(int i=0;i<teas.length;i++){
            Teacher tea = teas[i];
            if(tea != null && tea.getSid().equals(id)){
                index = i;
                break;
            }
        }
        return index;
    }
    //添加老师
    public boolean addTeacher(Teacher tea) {
        //添加老师到数组
        int index = -1;// 假设数组已经存满
        for(int i=0;i<teas.length;i++){
            Teacher teacher = teas[i];
            if(teacher == null){//如果数组没装满
                index = i;//用index记录当前下标
                break;
            }
        }
        if(index == -1){//装满了
            return false;
        }else{
            //没有装满 返回true,并且将老师对象添加到数组
            teas[index] = tea;
            return true;
        }
    }

    //查询全部老师
    public Teacher[] findAllTeacher() {
        return teas;
    }

    public void deleteTeacherById(String id) {
        int index = getIndex(id);//查询被删除id索引的位置
        teas[index] = null;//将索引位置使用null覆盖
    }

    public void updateTeacher(String updateId, Teacher newTea) {
        //找到要修改的索引位置
        int index = getIndex(updateId);
        teas[index] = newTea;

    }
}
